package com.atao.order.exception;

import com.atao.common.R;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

// 全局异常处理
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理Feign远程调用异常
     */
    @ExceptionHandler(FeignException.class)
    public R handleFeignException(FeignException e) {
        log.error("Feign远程调用异常: ", e);
        return R.error(500, "服务调用异常: " + e.getMessage());
    }

    /**
     * 处理参数校验异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public R handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数校验异常: ", e);
        return R.error(400, "参数错误: " + e.getMessage());
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public R handleNullPointerException(NullPointerException e) {
        log.error("空指针异常: ", e);
        return R.error(500, "系统内部错误");
    }

    /**
     * 处理运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(RuntimeException e) {
        log.error("运行时异常: ", e);
        return R.error(500, "系统错误: " + e.getMessage());
    }

    /**
     * 处理其他未捕获的异常
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        log.error("未捕获的异常: ", e);
        return R.error(500, "系统异常: " + e.getMessage());
    }
}
